/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.isd.global.jobs;

import java.io.Serializable;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedProperty;
import com.isd.global.modelo.Parametro;
import com.isd.global.rn.ParametrosRN;
import com.isd.pool.web.PoolAplicacionBean;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;


/**
*
* @author ctrosch
*/

@ManagedBean(eager=true)
@ApplicationScoped
public class JobManager implements Serializable {

    @EJB private ParametrosRN parametroRN;

    @ManagedProperty(value = "#{poolAplicacionBean}")
    private PoolAplicacionBean poolAplicacionBean;


    Parametro p;
    private ScheduledExecutorService scheduler;

    public JobManager() {

    }

    @PostConstruct
    public void init() {

        try {

            p = parametroRN.getParametros();
            scheduler = Executors.newSingleThreadScheduledExecutor();

            if(p.getModoPrueba().equals("S")){                
                scheduler.scheduleAtFixedRate(new ActualizarPoolesJob(poolAplicacionBean), 0, 1, TimeUnit.MINUTES);
            }else{
                scheduler.scheduleAtFixedRate(new ActualizarPoolesJob(poolAplicacionBean), 0, 1, TimeUnit.HOURS);
            }
            
        } catch (Exception e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "init", e);                                    
        }
        Logger.getLogger(getClass().getName()).log(Level.INFO, "init OK", "JobManager OK");            
    }

    @PreDestroy
    public void destroy() {
        
        scheduler.shutdownNow();
    }

    public PoolAplicacionBean getPoolAplicacionBean() {
        return poolAplicacionBean;
    }

    public void setPoolAplicacionBean(PoolAplicacionBean poolAplicacionBean) {
        this.poolAplicacionBean = poolAplicacionBean;
    }

    public ScheduledExecutorService getScheduler() {
        return scheduler;
    }

    public void setScheduler(ScheduledExecutorService scheduler) {
        this.scheduler = scheduler;
    }    
}
